package com.mall.shopping.services;

import com.mall.shopping.IProductCateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @program: mall-parent
 * @description:
 * @author: zhijie
 * @create: 2021-12-13 21:29
 **/
@Service
public class ProductCateServiceImpl implements IProductCateService {
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ProductCateConverter productCateConverter;

    @Override
    public AllProductCateResponse getAllProductCate() {
        AllProductCateResponse allProductCateResponse = new AllProductCateResponse();
        Example example = new Example(ItemCat.class);
        List<ProductCateDto> productCateDtos = null;
        try {
            List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
            productCateDtos = productCateConverter.items2Dto(itemCats);
        } catch (Exception e) {
            allProductCateResponse.setCode(ShoppingRetCode.SYSTEM_TIMEOUT.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.SYSTEM_TIMEOUT.getMessage());
            return allProductCateResponse;
        }
        allProductCateResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        allProductCateResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        allProductCateResponse.setProductCateDtoList(productCateDtos);
        return allProductCateResponse;
    }
}
